using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;

public class JumpButton : MonoBehaviour
{
    private GameObject Player;
    private Rigidbody2D playerRigidbody;
    private Animator playerAnimator;
    public float JumpSpeed;
    private BoxCollider2D feet;
    private bool isOnTheGround;
    // Start is called before the first frame update
    void Start()
    {
        Player = GameObject.Find("Player");
        playerRigidbody = Player.GetComponent<Rigidbody2D>();
        playerAnimator = Player.GetComponent<Animator>();
        Button btn = GetComponent<Button>();
        btn.onClick.AddListener(OnClick);
        feet = Player.GetComponent<BoxCollider2D>();
    }

    void Update()
    {
        CheckGround();
    }
    public void OnClick()
    {
        if (isOnTheGround)
        {
            Vector2 playerVel = new Vector2(playerRigidbody.velocity.y, JumpSpeed);
            playerAnimator.SetBool("Jumpping", true);
            playerRigidbody.velocity = playerVel;
        } else
        {
            playerAnimator.SetBool("Jumpping", false);
        }
    }
    void CheckGround()
    {
        isOnTheGround = feet.IsTouchingLayers(LayerMask.GetMask("Ground"));
    }
}
